/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import 'package:flutter/material.dart';
import 'package:hadss_geometry_transition/hadss_geometry_transition.dart';
import 'package:ohfeature_sample/components/full_screen_timer.dart';
import 'intents/intents_home_page.dart';
import 'knock_share/home/knock_share_home_page.dart';
import 'l10n/generated/app_localizations.dart';

class OhFeatureHomePage extends StatefulWidget {
  const OhFeatureHomePage({super.key});

  @override
  State<OhFeatureHomePage> createState() => _OhFeatureHomePageState();
}

class _OhFeatureHomePageState extends State<OhFeatureHomePage>
    with SingleTickerProviderStateMixin {
  // 当前选中的按钮索引，-1表示未选中
  int _selectedIndex = -1;
  // 页面路由
  final List<String> _pageRoutes = ['/KnockShareHomePage', '/IntentsHomePage'];
  @override
  void initState() {
    super.initState();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 预加载图片
    precacheImage(
        const AssetImage('lib/assets/images/full_screen_timer.png'), context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white10,
        elevation: 0,
      ),
      body: _buildContentView(),
    );
  }

  // 创建页面主体
  Widget _buildContentView() {
    // 获取本地化字符串
    AppLocalizations il8n = AppLocalizations.of(context)!;
    // 按钮文字
    final List<String> buttonTexts = [il8n.enterKnock, il8n.enterIntent];
    return Stack(
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Padding(
              padding: const EdgeInsets.only(bottom: 16),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  GeometryTransition(
                    closedBuilder: (context, open) {
                      return GestureDetector(
                          onTap: open,
                          child: _buildCustomButton(0, buttonTexts[0]));
                    },
                    openBuilder: (context, close) {
                      return const FullScreenOverlayTimer();
                    },
                    tappable: true,
                    onPushCallback: (data) => _handleButtonTap(0),
                  ),
                  GeometryTransition(
                    closedBuilder: (context, open) {
                      return GestureDetector(
                          onTap: open,
                          child: _buildCustomButton(1, buttonTexts[1]));
                    },
                    openBuilder: (context, close) {
                      return const FullScreenOverlayTimer();
                    },
                    tappable: true,
                    onPushCallback: (data) => _handleButtonTap(1),
                  )
                ],
              ),
            )
          ],
        ),
      ],
    );
  }

  // 创建按钮
  Widget _buildCustomButton(int index, String title) {
    final isSelected = _selectedIndex == index;
    return Container(
      margin: const EdgeInsets.only(left: 16, right: 16, bottom: 12),
      height: 40,
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: isSelected
              ? const Color.fromRGBO(10, 89, 247, 1)
              : const Color.fromRGBO(0, 0, 0, 0.05),
          borderRadius: BorderRadius.circular(20)),
      child: Text(
        title,
        style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
            color: isSelected
                ? Colors.white
                : const Color.fromRGBO(10, 89, 247, 1)),
      ),
    );
  }

  void _handleButtonTap(int index) {
    Navigator.pushNamed(context, _pageRoutes[index]);
    setState(() {
      if (_selectedIndex == index) {
        _selectedIndex = -1;
      } else {
        _selectedIndex = index;
      }
    });
  }
}
